package com.lmz.project.multitudethread.blockingqueuetest;

import java.util.concurrent.*;

public class BlockingQueueTest {

    public static BlockingQueue<String> queue = new LinkedBlockingQueue<>(10);   //声明一个容量为10的缓存队列
    public static void main(String[] args) throws InterruptedException {


        //new了三个生产者和一个消费者
        Producer producer1 = new Producer();
        Producer producer2 = new Producer();
        Producer producer3 = new Producer();
        Consumer consumer = new Consumer();

        //借助Executors
        ThreadPoolExecutor service = new ThreadPoolExecutor(
                5, 10, 1, TimeUnit.HOURS,
                new SynchronousQueue<Runnable>(),
                new ThreadPoolExecutor.AbortPolicy()
        );
        //启动线程
        service.execute(producer1);
        service.execute(producer2);
        service.execute(producer3);
        Thread.sleep(2000);
        service.execute(consumer);


        //执行10s
        Thread.sleep(10 * 1000);
        producer1.stop();
        producer2.stop();
        producer3.stop();

        Thread.sleep(2 * 1000);
        //退出Executor
        service.shutdown();

    }

}
